﻿using GN.Pay.Client.Utils;
using System.Threading.Tasks;

namespace GN.Pay.Client
{
    /// <summary>
    /// 服务客户端扩展
    /// </summary>
    public static class ServiceClientExtensions
    {
        /// <summary>
        /// 异步调用服务
        /// </summary>
        /// <param name="client">客户端</param>
        /// <param name="serviceName">服务名称</param>
        public static async Task CallServiceAsync(this IServiceClient client, string serviceName)
        {
            ExceptionUtils.CheckNotNull(client, nameof(client));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(serviceName, nameof(serviceName));
            await Task.Run(() =>
            {
                client.CallService(serviceName);
            });
        }

        /// <summary>
        /// 异步调用服务
        /// </summary>
        /// <typeparam name="TResult">结果类型</typeparam>
        /// <param name="client">客户端</param>
        /// <param name="serviceName">服务名称</param>
        /// <returns></returns>
        public static async Task<TResult> CallServiceAsync<TResult>(this IServiceClient client, string serviceName)
        {
            ExceptionUtils.CheckNotNull(client, nameof(client));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(serviceName, nameof(serviceName));
            return await Task.Run<TResult>(() =>
            {
                return client.CallService<TResult>(serviceName);
            });
        }

        /// <summary>
        /// 异步调用服务
        /// </summary>
        /// <typeparam name="TRequest">请求类型</typeparam>
        /// <param name="client">客户端</param>
        /// <param name="serviceName">服务名称</param>
        /// <param name="request"></param>
        public static async Task CallServiceAsync<TRequest>(this IServiceClient client, string serviceName, TRequest request)
            where TRequest : class
        {
            ExceptionUtils.CheckNotNull(client, nameof(client));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(serviceName, nameof(serviceName));
            await Task.Run(() =>
            {
                client.CallService(serviceName, request);
            });
        }

        /// <summary>
        /// 异步调用服务
        /// </summary>
        /// <typeparam name="TRequest">请求类型</typeparam>
        /// <typeparam name="TResult">响应类型</typeparam>
        /// <param name="client">客户端</param>
        /// <param name="serviceName">服务名称</param>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public static async Task<TResult> CallServiceAsync<TRequest, TResult>(this IServiceClient client, string serviceName, TRequest request) where TRequest : class
        {
            ExceptionUtils.CheckNotNull(client, nameof(client));
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(serviceName, nameof(serviceName));
            return await Task.Run<TResult>(() =>
            {
                return client.CallService<TRequest, TResult>(serviceName, request);
            });
        }
                
    }
}
